<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 后台管理员登录类
 * @author wwj
 *
 */
class Login extends MY_Controller
{
	/**
	 * 管理员锁定标记
	 * @var int
	 */
	const ISLOCKED = 1;
	function __construct()
	{
		parent::__construct();
		$this->load->model('manager_m');
	}

	/**
	 * 管理员后台登录默认方法
	 * 用户session存在，直接跳转到后台；否则打开登录页面
	 */
	public function index()
	{
		if ($this->check_login() === TRUE)
		{
			redirect(base_url('main'));
			return TRUE;
		}
	}

	/**
	 * 处理管理员登录
	 */
	public function login_action()
	{
		$post = $this->input->post();
		if ($post !== FALSE)
		{
			$login_name = $post['loginname'];
			$password   = $post['password'];
			$captcha    = $post['verify_code'];
			$password 	= strtolower(md5($password.SECRET_KEY));
			// 检验验证码
			if ($captcha != $this->session->userdata('captcha'))
			{
				$this->_show_message(base_url('login'), $this->lang->line('error_invalid_captcha'), 'goback');
				$this->session->unset_userdata('captcha');
				return ;
			}
			$this->session->unset_userdata('captcha');
			// Check the user name and password
			$manager = $this->manager_m->get_manager(array('loginname' => $login_name, 'password' => $password));
			if (empty($manager))
			{
				$this->_show_message(base_url('login'), $this->lang->line('error_invalid_admin'), 'goback');
				return ;
			}
			if (self::ISLOCKED == $manager['islocked'])
			{
				$this->_show_message(base_url('login'), $this->lang->line('error_admin_locked'), 'goback');
				return ;
			}
			
			// The user can login
			$this->do_login($manager);
			redirect(base_url('main'));
			return ;
		}
		else
		{
			$this->_show_message(base_url('login'), $this->lang->line('error_empty_post'), 'goback');
			return ;
		}
	}
	
	/**
	 * 处理登录信息，记录IP地址，登录时间等
	 * @param array $manager 管理员信息
	 * @return void|boolean
	 */
	protected function do_login($manager)
	{
		try
		{
			$data = array(
					'logintimes'     => $manager['logintimes'] + 1,
					'loginip'        => $this->input->ip_address(),
					'lastlogintime'  => $manager['logintime'] == '' ? '0' : $manager['logintime'],
					'logintime'      => time()
			);
			$this->manager_m->update_manager(array('id' => $manager['id']), $data);
				
			// 建立管理员session
			$session_date = array(
					'uid'       => $manager['id'],
					'shell'     => strtolower(md5($manager['id'].$manager['password'].SECRET_KEY)),
					'loginname' => $manager['loginname'],
					'logintime' => time()
			);
			$this->session->set_userdata($session_date);
			return TRUE;
		}
		catch (Exception $e)
		{
			$this->clear_admin_session();
			$this->_show_message(base_url('login'), $e->getMessage(), 'goback');
		}
	}
	
	/**
	 * 管理员退出登录状态
	 */
	public function logout()
	{
		return parent::logout($this->lang->line('message_logout_success'));
	}
	
	/**
	 * 显示验证码，直接调用显示
	 */
	public function captcha()
	{
		$this->load->library('captcha', array('code_type'=>'1'));
		$this->captcha->show();
		return ;
	}
	
    /**
     * 显示验证码，使用js调用
     */
    function show_script()
    {
    	//显示验证码
    	echo 'var img_src = "'.base_url('login/captcha/?')."\";\n";
    	echo "document.writeln('<img id=\"code\" src=\"' + img_src + Math.random() + '\" style=\"cursor:hand;\" onclick=\"this.src=img_src + Math.random();\" alt=\"点击更换图片\">');";
    }
	
}

/* End of file login.php */
/* Location: ./application/admin/controllers/login.php */